<?php


class SelectGroup {

    /**
     * Etiqueta o nombre del Grupo
     * @var String
     */
    private $label;

    /**
     * Opciones que contiene el Grupo
     * @var SelectOption
     */
    private $options;

    public function __construct($label) {
        $this->label = $label;
    }

    public function __toString() {
        return $this->_generateHtml();
    }

    private function _generateHtml() {
        $html = array();
        $params = 'label="' . $this->label . '" ';
        $html[] = '<optgroup ' . $params . '>';
        if (count($this->options)) {
            foreach ($this->options as $option) {
                $html[] = (String) $option;
            }
        }
        $html[] = '</optgroup>';
        $output = '';
        foreach ($html as $code) {
            $output = $output . $code;
        }
        return $output;
    }

    /**
     * Agrega Opcion al Grupo
     * @param SelectOption $option 
     */
    public function addOption(SelectOption $option) {
        $this->options[] = $option;
    }

    /**
     * Obtiene las Opciones del Grupo
     * @return SelectOption Arreglo de Opciones
     */
    public function getOptions() {
        return $this->options;
    }

}

?>
